package com.marveal.rabbit.producer.queue;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public abstract class AsyncBaseQueue {

    public static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();

    public static final long keepAliveTime = 60L;

    public static final int QUEUE_SIZE = 10000;

    private static final ExecutorService senderAsync =
            new ThreadPoolExecutor(CORE_POOL_SIZE,
                    CORE_POOL_SIZE,
                    keepAliveTime,
                    TimeUnit.SECONDS
                    , new ArrayBlockingQueue<>(QUEUE_SIZE),
                    runnable -> new Thread(runnable, "rabbitmq_client_async_sender"),
                    (runnable, executor) -> log.error("async sender is error rejected, runnable: {}, executor: {}", runnable, executor)
            );

    public static void submit(Runnable runnable) {
        senderAsync.submit(runnable);
    }

}
